package com.ctrl.riveradministrator.base;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ctrl.riveradministrator.http.RespModel;

public abstract class BaseFragment<P extends BasePresenter> extends Fragment
        implements View.OnClickListener, BaseView {

    protected final String TAG = this.getClass().getSimpleName();
    private View mContextView = null;
    protected P mPresenter;
    /**
     * handler
     */
    protected Handler handler = new Handler(this);
    // TODO: 2019/5/9 添加提示框

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContextView = inflater.inflate(bindLayout(), container, false);
        mPresenter = createPresenter();
        initView(mContextView);
        setListener();
        doBusiness(getContext(), getActivity());
        return mContextView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void onClick(View view) {
        if (fastClick()) {
            widgetClick(view);
        }
    }

    @Override
    public void showLoading() {
        // TODO: 2019/5/9 显示提示框
    }

    @Override
    public void hideLoading() {
        // TODO: 2019/5/9 隐藏提示框
    }

    @Override
    public void showError(String msg) {
        // TODO: 2019/5/9 显示错误
    }

    @Override
    public void onErrorCode(RespModel model) {

    }

    /**
     * 绑定布局
     *
     * @return 布局ID
     */
    protected abstract int bindLayout();

    /**
     * 初始化控件
     *
     * @param view view
     */
    protected abstract void initView(final View view);

    /**
     * 业务操作
     *
     * @param mContext context
     */
    protected abstract void doBusiness(Context mContext, Activity activity);

    /**
     * View点击
     *
     * @param view view
     */
    protected abstract void widgetClick(View view);

    /**
     * 设置监听
     */
    protected abstract void setListener();

    public abstract void handleMessage(Message message, int what);

    protected abstract P createPresenter();

    /**
     * 绑定控件
     *
     * @param view  当前view
     * @param resId 控件ID
     * @param <T>   控件类型
     * @return 控件类型
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T $(View view, int resId) {
        return (T) view.findViewById(resId);
    }

    /**
     * 防止过快点击
     *
     * @return true 正常点击，false 过快点击
     */
    private boolean fastClick() {
        long lastClick = 0;
        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }

    public static class Handler extends BaseHandler {

        private BaseFragment fragment;

        Handler(BaseFragment fragment) {
            super(fragment);
            this.fragment = fragment;
        }

        @Override
        public void handleMessage(Message msg, int what) {
            fragment.handleMessage(msg, what);
        }
    }
}
